package com.oss.robot.project;

import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.LinkStringFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

import com.oss.model.Project;
import com.oss.robot.BaseRobot;

/**
 * @author xueqiang.mi
 * @since 2010-7-28
 */
public class MuleSoftRobot extends BaseRobot {

	static public final String URL = "http://www.mulesoft.com/";
	static public final String SOURCE = "MuleSoft";
	static public final String LICENSE = "CPAL 1.0";

	public void retrieveProjects() throws ParserException {
		Parser parser = new Parser(URL);
		NodeList nodeList = parser.extractAllNodesThatMatch(new AndFilter(
				new TagNameFilter("ul"), new HasAttributeFilter("class",
						"globalnav")));
		nodeList = nodeList.elementAt(1).getChildren()
				.extractAllNodesThatMatch(new LinkStringFilter("/"), true);

		for (Node node : nodeList.toNodeArray()) {
			LinkTag linkTag = (LinkTag) node;
			String name = linkTag.getFirstChild().getText();
			String url = linkTag.getLink();

			Project prj = new Project(name, LICENSE, SOURCE, url);
			prj.setLabel(name);
			prj.setTag("others");
			prj.setEnabled(true);
			prjService.add(prj);
		}
	}

}
